package cn.test.Interceptor;

import java.util.Map;

import cn.test.util.GeneralRandom;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.config.entities.ResultConfig;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.interceptor.PreResultListener;

public class DownloadInterceptor extends AbstractInterceptor {
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		invocation.addPreResultListener(new PreResultListener() {

			@Override
			public void beforeResult(ActionInvocation invocation,
					String resultCode) {

				Map<String, ResultConfig> resultsMap = invocation.getProxy()
						.getConfig().getResults();
				ResultConfig finalResultConfig = resultsMap.get(resultCode);
				String fileName=GeneralRandom.getRandomString(10)+".xls";
				finalResultConfig.addParam("contentType",
						"application/vnd.ms-excel");
				finalResultConfig.addParam("contentDisposition",
						"attachment;filename="+fileName);
			}
		});
		return invocation.invoke();

	}

}
